package com.ohs.controller.before;

import com.ohs.pojo.User;
import com.ohs.service.OrderService;
import com.ohs.service.UserService;
import com.ohs.utils.Email;
import com.ohs.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Enumeration;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private OrderService orderService;


    //***************页面跳转**********
    //注册
    @GetMapping("/toReg")
    public String toReg(){
        return "before/reg";
    }

    //跳转到登录页面
    @GetMapping("/toLogin")
    public String toLoginPage(){
        return "before/login";
    }

    //邮箱登录
    @GetMapping("/toEmailLogin")
    public String emailLogin(){
        return "before/emailLogin";
    }

    //*****************操作************
    //检查邮箱是否可用
    @PostMapping("/checkEmail")
    @ResponseBody
    public ResponseResult checkEmail(String email){
        ResponseResult result = userService.findUserByEmail(email);
        return result;
    }

    //检查用户名是否可用
    @PostMapping("/checkUserName")
    @ResponseBody
    public ResponseResult checkUserName(String userName){
        ResponseResult result = userService.findUserByName(userName);
        return result;
    }

    //发送邮件，获取验证码
    @PostMapping("/sendEmail")
    @ResponseBody
    public ResponseResult sendEmail(String email, HttpSession session){
        String eCode =  Email.sendEmail(email);
        if (eCode==null){
            return new ResponseResult(201,"发送失败");
        }
        session.setAttribute("eCode",eCode);
        //System.out.println(verCode);
        ResponseResult responseResult = new ResponseResult(200,"发送成功");
        return responseResult;
    }

    //注册
    @PostMapping("/doReg")
    @ResponseBody
    public ResponseResult register(User user, String eCode, String confirmPwd, HttpSession session){
        return userService.doReg(user,confirmPwd,eCode, session);
    }

    //用户名登录
    @PostMapping("/doLogin")
    public String doLogin(User user, String code, HttpSession session, Model model){
        String view = userService.doLogin(user, code, session, model);
        return view;
    }

    //邮箱登录
    @PostMapping("/doEmailLogin")
    @ResponseBody
    public ResponseResult doEmailLogin(String email,String eCode,HttpSession session){
        return userService.doEmailLogin(email,eCode,session);
    }
    @PostMapping("/doUpdateInfo")
    @ResponseBody
    public ResponseResult doUpdateInfo(User user,HttpSession session){
        System.out.println("这里有数据吗？");
        return this.userService.updateUser(user,session);
    }
    @RequestMapping("/doUpdateAvartar")
    @ResponseBody
    public ResponseResult doUpdateAvartar(MultipartFile file, HttpSession session) throws IOException {
        System.out.println(file);
        return this.userService.doUpdateAvartar(file,session);
    }
    @RequestMapping("/toMygrxx")
    public String toMygraxx(Model model,HttpSession session){
        /*Integer userId=(Integer) session.getAttribute("userId");*/
        Integer userId=1;
        User user = this.userService.findUserOne(userId);
        model.addAttribute("user",user);
        return "before/mygrxx";
    }

    @RequestMapping("/toMygxin")
    public String toMygxin(Model model,HttpSession session){
        Integer userId=(Integer) session.getAttribute("userId");
        if(userId==null){
            return "redirect:toLogin";
        }
        int num =this.orderService.selectCountDaiShou(session);
        int num1= this.orderService.selectCountDaiYiShou(session);
        int num2 = this.orderService.selectCountUnpaid(session);
        model.addAttribute("daiShouHuo",num);
        model.addAttribute("yiShouHuo",num1);
        model.addAttribute("weizhifu",num2);
        return "before/mygxin";
    }
    //注销
    @GetMapping("/logOut")
    public String logOut(HttpServletRequest request){
        Enumeration em = request.getSession().getAttributeNames();
        while(em.hasMoreElements()){
            request.getSession().removeAttribute(em.nextElement().toString());
        }
        return "before/login";
    }

}
